<?php


class Login_Form_Login extends WSB_Xtension_Form
{

	/**
	 * @var boolean
	 */
	private $_emailAsUsername;

	/**
	 * Initialisierung
	 */
	public function init()
    {
    	$this->setName('login');

    	// Hidden (Action)
        $element = new Zend_Form_Element_Hidden('action');
        $element->setValue('login');
        //$element->setDecorators($this->hiddenDecorators);
        $this->addElement($element);

    	// Username
        $element = new Zend_Form_Element_Text('username');
        $element->setLabel('loginUsername');
        $element->setRequired(true);
        $element->setAutoInsertNotEmptyValidator(false);
        $notEmptyValidator = new Zend_Validate_NotEmpty();
        $notEmptyValidator->setMessages(array(
        	'isEmpty' => 'loginUsernameIsEmpty'
        ));
        $element->addValidator($notEmptyValidator);
        $element->addFilter(new Zend_Filter_StringTrim());
        //$element->setDecorators($this->elementDecorators);
		$this->addElement($element);

    	// Password
		$element = new Zend_Form_Element_Password('password');
        $element->setLabel('loginPassword');
        $element->setRequired(true);
        $element->setAutoInsertNotEmptyValidator(false);
        $notEmptyValidator = new Zend_Validate_NotEmpty();
        $notEmptyValidator->setMessages(array(
        	'isEmpty' => 'loginPasswordIsEmpty'
        ));
        $element->addValidator($notEmptyValidator);
        $element->addFilter(new Zend_Filter_StringTrim());
        //$element->setDecorators($this->elementDecorators);
		$this->addElement($element);

    	// Button
        $element = new Zend_Form_Element_Submit('btnLogin');
        $element->setLabel('loginSubmit');
        $element->setAttrib('class', 'submit-button-save');
        //$element->setDecorators($this->buttonDecorators);
		$this->addElement($element);

    }

	/**
	 * @param boolean $flag
	 */
	public function setUseEmailAsUsername($flag)
	{
		$this->_emailAsUsername = (bool)$flag;
		if($this->useEmailAsUsername()) {
			$this->replaceUsernameWithEmail();
		}
	}

	/**
	 * @return boolean
	 */
	public function useEmailAsUsername()
	{
		return $this->_emailAsUsername;
	}

	/**
	 * @return void
	 */
	public function replaceUsernameWithEmail()
	{
		$order = $this->getElement('username')->getOrder();
		$this->removeElement('username');

    	// E-Mail
        $element = new Zend_Form_Element_Text('username');
        $element->setLabel('loginEmail');
        $element->setRequired(true);
        $element->setAutoInsertNotEmptyValidator(false);
        $notEmptyValidator = new Zend_Validate_NotEmpty();
        $notEmptyValidator->setMessages(array(
        	'isEmpty' => 'loginEmailIsEmpty'
        ));
        $element->addValidator($notEmptyValidator, true);
        $emailValidator = new WSB_Validator_EmailAddressSimple();
		$emailValidator->setMessages(array(
			WSB_Validator_EmailAddressSimple::INVALID => 'loginInvalidEmail'
    	));
        $element->addValidator($emailValidator);
        $element->addFilter(new Zend_Filter_StringTrim());
		//$element->setDecorators($this->elementDecorators);
        $element->setOrder($order);
		$this->addElement($element);
	}

	/**
	 * @param string $redirectUrl
	 */
	public function addRequestHiddenElement($redirectUrl)
	{
        $element = new Zend_Form_Element_Hidden('redirect');
        $element->setValue($redirectUrl);
        //$element->setDecorators($this->hiddenDecorators);
        $this->addElement($element);
	}

}
